iT邦幫忙

0

【C#】Multi Value Return

c#
  • 分享至 

  • xImage
  •  

這次要來學習如何讓函式返回多個值~

分別用Array~ Struct~ Tuple~ Output Parameters~ Reference~ ValueTuple~


學習目標: Multi Value Return 實務

學習難度: ☆☆☆


Returning by Array

using System;

namespace ConsoleApp1
{
    class MainProgram
    {
        static int[] MultipleReturns(int score1, int score2)
        {
            int[] array = new int[2];

            if (score1 > score2)
            {
                array[0] = score2;

                array[1] = score1;
            }

            if (array[0] < array[1])
            {
                array[0] = score1;

                array[1] = score2;
            }
            return array;
        }

        static void Main()
        {
            int[] array = MultipleReturns(90, 70);
        }
    }
}

Returning by Struct or Class

using System;

namespace ConsoleApp1
{
    struct Scores
    {
        public int score1;

        public int score2;
    }

    class MainProgram
    {
        static Scores MultipleReturns(int score1, int score2)
        {
            Scores scores = new Scores();

            if (score1 > score2)
            {
                scores.score1 = score2;

                scores.score2 = score1;
            }

            if (score1 < score2)
            {
                scores.score1 = score1;

                scores.score2 = score2;
            }

            return scores;
        }

        static void Main()
        {
            Scores scores = new Scores();

            scores = MultipleReturns(90, 70);
        }
    }
}

Returning by Tuple

using System;

namespace ConsoleApp1
{
    class MainProgram
    {
        static Tuple<int, int> MultipleReturns(int score1, int score2)
        {
            if (score1 > score2)
            {
                int temp = score2;

                score2 = score1;

                score1 = temp;
            }
            return new Tuple<int, int>(score1, score2);
        }

        static void Main()
        {
            var tuple = MultipleReturns(90, 70);
        }
    }
}

Returning by Output Parameters

using System;

namespace ConsoleApp1
{
    class MainProgram
    {
        static void MultipleReturns(out int score1, out int score2, out int avg)
        {
            score1 = 60;

            score2 = 100;

            avg = (score2 + score1) / 2;
        }

        static void Main()
        {
            int score1, score2, avg;

            MultipleReturns(out score1, out score2, out avg);
        }
    }
}

Returning by Ref


using System;

namespace ConsoleApp1
{
    class MainProgram
    {
        static void MultipleReturns(ref int score1, ref int score2, ref int avg)
        {
            score1 = 60;

            score2 = 100;

            avg = (score2 + score1) / 2;
        }

        static void Main()
        {
            int score1 = 0, score2 = 0, avg = 0;

            MultipleReturns(ref score1, ref score2, ref avg);
        }
    }
}

Returning by Value Tuple (需用NuGet裝System.ValueTuple)

using System;

namespace ConsoleApp1
{
class MainProgram
{
static (int, int) MultipleReturns(int score1, int score2)
{

        if (score1 > score2)
        {
            int temp = score2;

            score2 = score1;

            score1 = temp;
        }

        return (score1, score2);
    }

    static void Main()
    {
        (int, int) output = MultipleReturns(90, 70);

        Console.WriteLine(output.Item1 + "" + output.Item2);
    }
}

}


-----

參考資料:

https://www.c-sharpcorner.com/UploadFile/9b86d4/how-to-return-multiple-values-from-a-function-in-C-Sharp/

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
jakeuj
iT邦新手 5 級 ‧ 2022-04-11 14:28:03

感謝大大的指點~

ValueTuple的確比Tuple精簡多了 !

/images/emoticon/emoticon08.gif

我要留言

立即登入留言